﻿using System.Collections.Generic;
using System.Web;
using System.Web.Http;
using AutoMapper;
using LsTool;
using LsTool.ApiModels;
using X.PagedList;

namespace Zapi.Api
{
    public class ZapiController : ApiController
    {
        /// <summary>
        /// token
        /// </summary>
        protected string Token;
         
        /// <summary>
        /// 
        /// </summary>
        public ZapiController()
        {
            
            Token = HttpContext.Current.Request.Headers["token"];

        }

        /// <summary>
        /// 错误
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        protected MyError ToError(string msg, string code = "510")
        {
            return new MyError(msg, code);
        }
       
        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected BaseOut ToOk(string msg = "")
        {
            return new BaseOut()
            {
                Code = "0",
                Msg = msg
            };
        }
        /// <summary>
        /// 返回一般模型
        /// </summary>
        /// <typeparam name="D"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        protected D To<D>(object t) where D : BaseOut
        {
            return Map<D>(t);
        }

        protected ListOut<T> ToList<T>(IEnumerable<T> list)
        {
            return new ListOut<T>(list);

        }

        protected PagedOut<T> ToPagedList<T>(IPagedList<T> list)
        {
            return new PagedOut<T>(list);
        }
        /// <summary>
        /// 返回分页
        /// </summary>
        /// <typeparam name="D"></typeparam> 
        /// <typeparam name="S">源</typeparam> 
        /// <param name="list"></param>
        /// <returns></returns>
        protected ListOut<D> ToList<S, D>(IEnumerable<S> list)
        {
            var lo = new ListOut<S>(list);
            return Map<ListOut<D>>(lo);
            
        }

        /// <summary>
        /// 返回分页
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <typeparam name="D"></typeparam> 
        /// <param name="pagedList"></param>
        /// <returns></returns>
        protected PagedOut<D> ToPagedList<S, D>(IPagedList<S> pagedList)
        {
            var pageCount = pagedList.PageCount;
            var totalItemCount = pagedList.TotalItemCount;
            IEnumerable<D> list = Map<IEnumerable<D>>(pagedList);
            return new PagedOut<D>(list, pageCount, totalItemCount);
        }
        /// <summary>
        /// 返回分页
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <typeparam name="D"></typeparam> 
        /// <param name="pagedList"></param>
        /// <param name="list">更新后的list</param>
        /// <returns></returns>
        protected PagedOut<D> ToPagedList<S, D>(IPagedList<S> pagedList, IEnumerable<D> list)
        {
            var staticPagedList = new StaticPagedList<D>(list, pagedList);
            return new PagedOut<D>(staticPagedList);
        }

        /// <summary>
        /// 模型转换
        /// </summary> 
        /// <param name="s"></param> 
        /// <returns></returns>
        protected D Map<D>(object s)
        {
            return Mapper.Map<D>(s);
        }

        /// <summary>
        /// 模型转换
        /// </summary> 
        /// <param name="s"></param> 
        /// <returns></returns>
        protected D Map<D>(object s, D d)
        {
            return Mapper.Map(s, d);
        }

    }
}